<script setup>
import { getGoodsid, buygoodService } from '/src/api/layout.js'
import { addshopcart } from '/src/api/shopcart.js'
import { useUserStore } from '@/stores'
import { ref } from 'vue'
import { useRoute } from 'vue-router'
// import { route } from 'vue-router'
const doi = ref()
const userStore = useUserStore()
const route = useRoute()
const data = ref([])
const addcart = async () => {
  const res = await addshopcart(route.params.id)
  if (res.code == 1) {
    ElMessage.success(res.data)
  } else {
    ElMessage.error(res.data)
  }
  // await userStore.getUser()
  // location.reload()
}
const {
  user: { balance }
} = useUserStore()
const getidgood = async () => {
  const getgoodsid = route.params.id
  const response = await getGoodsid(getgoodsid)
  data.value = response.data
  if (data.value.status == 2) {
    doi.value = '已被购买'
  } else {
    doi.value = '正常'
  }
}
getidgood()
const buyGoods = async () => {
  await ElMessageBox.confirm(`你确认购买？您的余额:${balance}`, '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  const res = await buygoodService(data.value.id)
  console.log(res)
  if (res.code == 1) {
    ElMessage.success(res.data)
  } else {
    ElMessage.error(res.data)
  }
  await userStore.getUser()
  getidgood()
}
</script>
<template>
  <div class="xtx-goods-page">
    <div class="container">
      <!-- 商品信息 -->
      <div class="info-container">
        <div>
          <div class="goods-info">
            <div class="media">
              <!-- 图片预览区 -->
              <img v-if="data.image" :src="data.image" alt="" />
              <img v-else src="@/assets/empty.png" alt="" />
              <!-- 统计数量 -->
              <ul class="goods-sales">
                <li>
                  <p>商品ID</p>
                  <p>{{ data.id }}</p>
                  <p><i class="iconfont icon-task-filling"></i>商品ID</p>
                </li>
                <li>
                  <p>商品分类</p>
                  <p>{{ data.categoryName }}</p>
                  <p><i class="iconfont icon-comment-filling"></i>商品分类</p>
                </li>
                <li>
                  <p>商品卖家</p>
                  <p>{{ data.sellerName }}</p>
                  <p><i class="iconfont icon-favorite-filling"></i>商品卖家</p>
                </li>
                <li>
                  <p>商品状态</p>
                  <p>{{ doi }}</p>
                  <p><i class="iconfont icon-dynamic-filling"></i>商品状态</p>
                </li>
              </ul>
            </div>

            <div class="spec">
              <!-- 商品信息区 -->
              <p class="g-name"><strong>商品名称:</strong>{{ data.name }}</p>
              <p class="g-desc">商品状态:{{ doi }}</p>
              <p class="g-price">
                <span>{{ data.price }}</span>
              </p>
              <div class="g-service">
                <dl>
                  <dt>卖家</dt>
                  <dd>
                    <el-avatar :size="40" :src="data.userImage" />
                    {{ data.sellerName }}
                  </dd>
                </dl>
                <dl>
                  <dt>服务</dt>
                  <dd>
                    <span>良好服务</span>
                    <span>快速送达</span>
                    <span>免费包邮</span>
                    <span class="phone">电话:{{ data.sellerPhone }}</span>
                  </dd>
                </dl>
                <dl>
                  <dt>商品简介</dt>
                  <dd>{{ data.description }}</dd>
                </dl>
              </div>
              <div>
                <el-button
                  size="large"
                  class="btn"
                  type="primary"
                  @click="addcart"
                >
                  加入购物车
                </el-button>
                <el-button
                  size="large"
                  class="btn"
                  type="primary"
                  @click="buyGoods"
                >
                  立即购买
                </el-button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
img {
  max-height: 450px;
  width: 100%;
  border: 5px solid gray;
}
button {
  height: 50px;
  width: 100px;
  margin-top: 50px;

  margin-right: 150px;
}
.xtx-goods-page {
  .goods-info {
    box-shadow: 10px 5px 5px 0px gray;
    min-height: 200px;
    background: #fff;
    display: flex;

    .media {
      width: 580px;
      height: 600px;
      padding: 30px 50px;
    }

    .spec {
      flex: 1;
      padding: 30px 30px 30px 0;
    }
  }

  .goods-tabs {
    min-height: 600px;
    background: #fff;
  }

  .goods-warn {
    min-height: 600px;
    background: #fff;
    margin-top: 20px;
  }

  .number-box {
    display: flex;
    align-items: center;

    .label {
      width: 60px;
      color: #999;
      padding-left: 10px;
    }
  }

  .g-name {
    font-size: 22px;
  }

  .g-desc {
    color: #999;
    margin-top: 10px;
  }

  .g-price {
    margin-top: 10px;

    span {
      &::before {
        content: '¥';
        font-size: 14px;
      }

      &:first-child {
        color: red;
        margin-right: 10px;
        font-size: 22px;
      }
    }
  }

  .g-service {
    background: #f5f5f5;
    width: 500px;
    padding: 20px 10px 0 10px;
    margin-top: 10px;

    dl {
      padding-bottom: 20px;
      display: flex;
      align-items: center;

      dt {
        width: 50px;
        color: #999;
      }

      dd {
        color: #666;

        &:last-child {
          span {
            margin-right: 10px;

            &::before {
              color: red;
              margin-right: 2px;
            }
          }

          .phone {
            color: green;
          }
        }
      }
    }
  }

  .goods-sales {
    display: flex;
    width: 400px;
    align-items: center;
    text-align: center;
    height: 140px;

    li {
      flex: 1;
      position: relative;

      ~ li::after {
        position: absolute;
        top: 10px;
        left: 0;
        height: 60px;
        border-left: 1px solid #e4e4e4;
        content: '';
      }

      p {
        &:first-child {
          color: #999;
        }

        &:nth-child(2) {
          color: red;
          margin-top: 10px;
        }

        &:last-child {
          color: #666;
          margin-top: 10px;

          i {
            color: red;
            font-size: 14px;
            margin-right: 2px;
          }

          &:hover {
            color: black;
            cursor: pointer;
          }
        }
      }
    }
  }
}

.btn {
  margin-top: 20px;
}
</style>
